package com.aizhixin.lab.docker.common;

import com.aizhixin.lab.account.dto.AccountDTO;
import com.aizhixin.lab.config.Config;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.*;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;

import java.util.Map;


@Component
public class AuthUtilService {
	@Autowired
	private RedisTemplate<String,String> redisTemplate;
	@Autowired
	private Config config;


	private String TYPE_TOKEN = "token:";

	public AccountDTO getUserByToken(String token) {
		AccountDTO dto = getAccountDTOFromRedis(token);
//		if (dto == null) {
//			dto = getAccountDTO(token);
//			if (dto != null) {
//				setRedisAccountDTO(token, dto);
//			}
//		}
		return dto;
	}
	private AccountDTO getAccountDTOFromRedis(String token) {
		String str = (String) redisTemplate.opsForValue().get(TYPE_TOKEN + token.replace("Bearer ", ""));
		if (!StringUtils.isEmpty(str)) {
			return JSON.parseObject(str, AccountDTO.class);
		}
		return null;
	}
//	public AccountDTO getSsoUserInfo(String token) {
//		RestTemplate rest = new RestTemplate();
//		HttpHeaders headers = new HttpHeaders();
//		List<MediaType> mt = new ArrayList<MediaType>();
//		mt.add(MediaType.APPLICATION_JSON_UTF8);
//		headers.set(HttpHeaders.AUTHORIZATION, token);// {"access_token":"42c5852c-bdaf-4cea-a09c-0cab54988120","token_type":"bearer","refresh_token":"e4fa3c77-6041-482a-bc5a-2e0d2e5c48e4","expires_in":13933,"scope":"read
//		headers.set("Encoding", "UTF-8");
//		headers.setAccept(mt);// {"access_token":"eba06c1c-84b9-4706-a27f-0dd11bcc30bc","token_type":"bearer","refresh_token":"384aba1d-925c-4ffb-94f3-c674e550c79e","expires_in":85989,"scope":"read
//								// write"}
//		System.out.println(token);
//		AccountDTO dto = null;
//		HttpEntity<byte[]> entity = new HttpEntity<byte[]>(headers);
//		try {
//			ResponseEntity<AccountDTO> response = rest.exchange(dleduBackHost
//					+ userInfoUrl, HttpMethod.GET, entity, AccountDTO.class);
//			if (HttpStatus.SC_OK != response.getStatusCode().value()) {
//				System.out.println(response.getStatusCode().value());
//			} else {
//				dto = response.getBody();
//			}
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//
//		return dto;
//	}






}
